package com.reportgenerate.marketreport.controller;

import com.reportgenerate.marketreport.entity.ResultData;
import com.reportgenerate.marketreport.entity.model.Customer;
import com.reportgenerate.marketreport.entity.model.CustomerExample;
import com.reportgenerate.marketreport.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @program: reportgenerate
 * @Description: 客户信息接口
 * @author: Mr.Cheng
 * @Email: ccelephant_518@126.com
 * @date: 2023/8/20 20:25
 */
@RestController
@RequestMapping("/customer")
@Api(tags = "客户服务")
public class CustomerController {
    private CustomerService customerService;
    @Autowired
    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @ApiOperation("获取客户信息列表")
    @GetMapping("/customers")
    public ResultData<List<Customer>> getCustomerInformationList() {
        List<Customer> customerList = customerService.getCustomerInformationList();
        return ResultData.OK(customerList);
    }

    /**
    * @Description: 获取客户信息分页，page从0开始，size为每页的大小
    * @Param: [page, size]
    * @return: java.util.HashMap
    * @Author: Mr.Cheng
    * @Email: ccelephant_518@126.com
    * @Date: 2023/8/20 23:16
    */
    @ApiOperation("获取客户信息分页")
    @PostMapping ("/customers/page")
    public ResultData<HashMap> getCustomerInformationListByPage(@ApiParam("页码") @RequestParam("page") int page, @ApiParam("页面数据量") @RequestParam("size") int size) {
        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andIdIsNotNull();
        customerExample.setOrderByClause("id desc");
        HashMap customerMap = customerService.getCustomerInformationListByPage(customerExample, page, size);
        return ResultData.OK(customerMap);
    }

    /**
    * @Description: 插入客户信息
    * @Param: [customer]
    * @return: int
    * @Author: Mr.Cheng
    * @Email: ccelephant_518@126.com
    * @Date: 2023/8/20 23:18
    */
    @ApiOperation("插入客户信息")
    @PostMapping("/customer")
    public ResultData<Integer> insertCustomerInformation(@ApiParam("客户信息") @RequestBody Customer customer) {
        Integer insertResult = customerService.insertCustomerInformation(customer);
        return ResultData.OK(insertResult);
    }

    /**
    * @Description: 删除客户信息
    * @Param: [customer]
    * @return: int
    * @Author: Mr.Cheng
    * @Email: ccelephant_518@126.com
    * @Date: 2023/8/22 16:53
    */
    @ApiOperation("删除客户信息")
    @DeleteMapping("/customer")
    public ResultData<Integer> deleteCustomerInformation(@ApiParam("客户信息") @RequestBody Customer customer) {
        Integer deleteResult = customerService.deleteCustomerInformationById(customer.getId());
        return ResultData.OK(deleteResult);
    }
}
